package com.eascs.web.p2p.action;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.common.entity.MsgTemplateEntity;
import com.eascs.p2p.common.service.IMsgTemplateService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.p2p.utils.StringUtils;

@RestController
@RequestMapping("/templateRest")
public class TemplateRestController extends BaseRestController {

	@Reference
	IMsgTemplateService msgTemplateService;

	@RequestMapping(value = "/save_template")
	public BaseRestResult<String> saveTemplate(MsgTemplateEntity vo) {
		String templateCode = "";
		if(org.springframework.util.StringUtils.isEmpty(vo.getTemplateContent())){
			return BaseRestResult.warn("模板内容不能为空");
		}
		int time = StringUtils.getTime();
		if (StringUtil.isNotBlank(vo.getId())) {
			MsgTemplateEntity entity = new MsgTemplateEntity();
			ModelResult<MsgTemplateEntity> entityModel = msgTemplateService.queryById(vo.getId());
			if (entityModel.isSuccess()) {
				entity = entityModel.getModel();
			}
			entity.setTemplateName(vo.getTemplateName());
			entity.setTemplateType(vo.getTemplateType());
			entity.setTimesPerHour(vo.getTimesPerHour());
			entity.setTimesPerDay(vo.getTimesPerDay());
			entity.setTemplateContent(vo.getTemplateContent());
			
			ModelResult<Boolean> model = msgTemplateService.update(entity);
			if (model.isSuccess()) {
				if (model.getModel()) {

					return BaseRestResult.warn("修改成功").andRedirect("/template/index");
				} else {
					return BaseRestResult.error("数据库操作失败");
				}
			} else {
				return BaseRestResult.info("修改失败");
			}
		} else {
			if (vo.getTemplateType() == 0) {
				templateCode = "DX-" + time;
			} else if (vo.getTemplateType() == 1) {
				templateCode = "YJ-" + time;
			} else if (vo.getTemplateType() == 2) {
				templateCode = "WB-" + time;
			} else if (vo.getTemplateType() == 3) {
				templateCode = "WX-" + time;
			}
			vo.setTemplateCode(templateCode);
			if(vo.getTimesPerDay()==0){
				vo.setTimesPerDay(10);
			}
			
			if(vo.getTimesPerHour()==0){
				vo.setTimesPerHour(3);
			}
			ModelResult<Boolean> savemodel = msgTemplateService.add(vo);
			if (savemodel.isSuccess()) {
				if (savemodel.getModel()) {

					return BaseRestResult.warn("添加成功").andRedirect("/template/index");
				} else {
					return BaseRestResult.error("数据库操作失败");
				}
			} else {
				return BaseRestResult.info("添加失败失败");
			}
		}
	}

	/**
	 * @Title: isDisplay
	 * @Description:控制前台是否显示
	 * @author 张庆奇
	 * @param id
	 * @param isDisplay
	 * @return
	 * @return BaseRestResult<String>
	 * @throws @date
	 *             2016年7月11日 下午4:55:39
	 */
	@RequestMapping(value = "/isDisplay")
	public BaseRestResult<String> isDisplay(String id, int isDisplay) {
		BaseRestResult<String> result = null;
		boolean isLockflag = true;
		if (isDisplay == 1) {
			isLockflag = false;
		}
		ModelResult<Boolean> flag_model = msgTemplateService.lock(id, isLockflag);
		boolean flag = false;
		if (flag_model.isSuccess()) {
			flag = flag_model.getModel();
		}

		if (flag) {
			result = BaseRestResult.warn("操作成功").andRedirect("/template/index");
		} else {
			result = BaseRestResult.error("操作失败");
		}

		return result;

	}

}
